package com.ang.jdk8.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author ssang
 * @date 2021-09-26 13:50
 */
public class IntStreamTest {
    public static void main(String[] args) {
        IntStreamTest t = new IntStreamTest();
//        t.testList();
//        t.testStringBuilder();
        t.test01();
    }

    public void test(){
        IntStream.range(0,10).forEach(
            i->System.out.println(i)
        );
    }

    public void testList(){
        long t1 = System.currentTimeMillis();
        List<String> list = new ArrayList<>();
        IntStream.range(0,10000000).forEach(
            i->{
                list.add(i+"");
            }
        );
        String[] arr = (String[]) list.toArray(new String[0]);
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
    }

    public void testStringBuilder(){
        long t1 = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        IntStream.range(0,10000000).forEach(
                i->{
                    sb.append(i+",");
                }
        );
        String[] arr = sb.toString().split(",");
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
    }

    public void test01(){
        List<Integer> list = new ArrayList<>();
        IntStream.range(0,100000000).forEach(
                i->{
                    list.add(i);
                }
        );

        long t1 = System.currentTimeMillis();
        list.stream().forEach(
                obj->{
                    if(obj/2==0){}
                }
        );
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);

        t1 = System.currentTimeMillis();
        list.stream()
                .filter(l->l/2==0)
                .forEach(obj->{});
        t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
    }
}
